# -- coding: utf-8 --
import re
import json
import scrapy
import requests
from spidertools.utils.time_utils import get_current_date
from commonresources.spider_items.base_item import convert_dict
from commonresources.spider_items.hunan.items import HuNanShengShuiLiGongChengZhaoTouBiaoGongGongFuWuPingTaiItem
from commonresources.spiders.basespider import BaseSpider


class HuNanShengShuiLiGongChengZhaoTouBiaoGongGongFuWuPingTaiSpider(BaseSpider):
    """
        湖南省水利工程招投标公共服务平台     http://slba.hnsggzy.com/
    """

    def parse(self, response):
        pass

    name = 'HuNanShengShuiLiGongChengZhaoTouBiaoGongGongFuWuPingTai'
    name_zh = "湖南省水利工程招投标公共服务平台"
    province = "湖南"
    allowed_domains = ['slba.hnsggzy.com']

    def __init__(self, full_dose=True):
        super(HuNanShengShuiLiGongChengZhaoTouBiaoGongGongFuWuPingTaiSpider, self).__init__(full_dose)
        self.convert_dict = convert_dict  # 存储时转化用

    @property
    def fake_headers(self):
        headers = {

            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
                          " Chrome/86.0.4240.111 Safari/537.36 Edg/86.0.622.58",
        }
        return headers

    def start_requests(self):
        list_type = ["ZBTBXXZBGG", "ZBJGGG"]
        tan = [v_0 for v_0 in list_type]
        san = [value for value in range(1, 60)]
        for value in san:
            for v_0 in tan:
                code = "Code=CMS_GetContentByCode&page="+str(value)+"&pagesize=45&sortname=EFFECTDATE%20desc," \
                       "%20EXTENSION2%20desc&sortorder=&Where={%22op%22:%22and%22,%22rules%22:[{%22op%22:%22equal%22," \
                       "%22field%22:%22CATEGORYFULLCODE%22,%22value%22:%22ZTB_"+str(v_0)+"%22,%22type%22:%22string%22}]} "
                url = "http://slba.hnsggzy.com/api/SysQuery/get?" + code
                yield scrapy.Request(url=url, callback=self.handle_response)

    def handle_response(self, response):
        url = response.url
        r = requests.get(url).text
        response_obj = json.loads(r)
        j = response_obj["Rows"]
        for row in j:
            item = {}
            item["announcement_title"] = row['CONTENTTITLE']
            item["release_time"] = row["EFFECTDATE"][:10]
            if "ZTB_ZBTBXXZBGG" in row["CATEGORYFULLCODE"]:
                item['announcement_type'] = "招标公告"
            else:
                item['announcement_type'] = "中标公告"
            url_1 = row["CONTENTID"]
            item["origin_url"] = 'http://slba.hnsggzy.com/Homes/ZTB/pages/' + url_1 + ".htm"
            item["item"] = HuNanShengShuiLiGongChengZhaoTouBiaoGongGongFuWuPingTaiItem()
            yield scrapy.Request(url=item["origin_url"],
                                 callback=self.parse_item_new,
                                 meta=item)